package com.cxg.fairy.ec.main.order;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.personal.favor.ShopCartGoodsListDataConverter;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/18.
 */

class PostOrderGoodsListDelegate extends LatteDelegate implements ISuccess{

    private final static String POST_ORDER_ID = "POST_ORDER_ID" ;

    @BindView(R2.id.rv_favor_list)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.titleText)
    TextView titleView = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    public static PostOrderGoodsListDelegate create(int orderId) {
        final Bundle args = new Bundle();
        args.putInt(POST_ORDER_ID, orderId);
        PostOrderGoodsListDelegate delegate = new PostOrderGoodsListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_post_order_goods_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("getShopCartList")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }


    @Override
    public void onSuccess(String response) {
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this.getContext(),DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.my_decoration_single));
        mRecyclerView.addItemDecoration(dividerItemDecoration);

        final List<MultipleItemEntity> data = new ShopCartGoodsListDataConverter().setJsonData(response).convert();
        final PostOrderGoodsListAdapter postOrderAdapter = new PostOrderGoodsListAdapter(data);
        mRecyclerView.setAdapter(postOrderAdapter);
    }
}
